import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/cupertino.dart';
import '../../home/home_screen.dart';

class LoginFormController {
  final _formKey = GlobalKey<FormState>();
  final _usernameController = TextEditingController();
  final _passwordController = TextEditingController();
  bool _obscurePassword = true;
  bool _rememberMe = false;
  bool _isLoading = false;

  GlobalKey<FormState> get formKey => _formKey;
  TextEditingController get usernameController => _usernameController;
  TextEditingController get passwordController => _passwordController;
  bool get obscurePassword => _obscurePassword;
  bool get rememberMe => _rememberMe;
  bool get isLoading => _isLoading;

  void togglePasswordVisibility() {
    _obscurePassword = !_obscurePassword;
  }

  void toggleRememberMe(bool? value) {
    _rememberMe = value ?? false;
  }

  Future<void> handleLogin(BuildContext context) async {
    if (_formKey.currentState!.validate()) {
      _isLoading = true;
      
      // كىرىشتە تىترەش ئانىماتسىيەسى
      HapticFeedback.mediumImpact();
      
      // كىرىش ئانىماتسىيەسى 1 سېكۇنت
      await Future.delayed(const Duration(milliseconds: 1000));
      
      if (context.mounted) {
        // كىرىش مۇۋەپپەقىيەتلىك بولغاندا، باش بەتكە يۆتكەش
        Navigator.of(context).pushReplacement(
          PageRouteBuilder(
            transitionDuration: const Duration(milliseconds: 500),
            pageBuilder: (context, animation, secondaryAnimation) => const HomeScreen(),
            transitionsBuilder: (context, animation, secondaryAnimation, child) {
              var begin = const Offset(1.0, 0.0);
              var end = Offset.zero;
              var curve = Curves.ease;
              var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
              return SlideTransition(
                position: animation.drive(tween),
                child: child,
              );
            },
          ),
        );
      }
    }
  }

  void dispose() {
    _usernameController.dispose();
    _passwordController.dispose();
  }
} 